package com.library.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.library.dao.DAOFactory;
import com.library.dao.MySQLDAOFactory;
import com.library.dao.User;
import com.library.dao.UserDAO;

/**
 * Servlet implementation class LibrarianUpdateUserInfo
 */
@WebServlet("/LibrarianUpdateUserInfo")
public class LibrarianUpdateUserInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LibrarianUpdateUserInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		int userid = Integer.parseInt(request.getParameter("userid"));
		String email = request.getParameter("email");
		String name = request.getParameter("username");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		String password = request.getParameter("password");
		
		DAOFactory factory = new MySQLDAOFactory();
		UserDAO userDAO = factory.getUserDAO();
		User user = userDAO.findUser(userid);
		if (email != "") {
			user.setEmail(email);
		}
		if (name != "") {
			user.setName(name);
		}
		if (address != "") {
			user.setAddress(address);
		}
		if (phone != "") {
			user.setPhone(phone);
		}
		if (password != "") {
			user.setPassword(password);
		}
		userDAO.updateUser(user);
		request.setAttribute("msg", "Bạn đã cập nhật thông tin thành công");
		request.setAttribute("lastpage", "librarian/updateuserinfo.jsp");
		String add = "librarian/librariansuccesspage.jsp";
		 RequestDispatcher dispatcher =
			      request.getRequestDispatcher(add);
			    dispatcher.forward(request, response);
	}

}
